package com.compass.doctor.ui.message

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.databinding.ActivityNotificationBinding
import com.compass.doctor.ui.message.adapter.NotificationAdapter
import com.compass.doctor.ui.message.viewmodel.MessageViewModel
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.constant.REFRESH_MESSAGE
import com.compass.framework.constant.REFUSE_RECEPTION
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.utils.CheckNotifyPermissionUtils.checkNotifyPermission
import com.compass.framework.utils.CheckNotifyPermissionUtils.tryJumpNotifyPage
import com.compass.framework.utils.LiveDataBus

import com.compass.framework.utils.StatusBarSettingHelper
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 消息通知
 * Date:2023/12/7 14:32
 */
class NotificationActivity : BaseVBActivity<ActivityNotificationBinding>(), OnRefreshListener,
    OnLoadMoreListener {


    private val messageViewModel: MessageViewModel by viewModels()
    private lateinit var mAdapter: NotificationAdapter

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, NotificationActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var mPage = 1
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarSettingHelper.setStatusBarTranslucent(this)
        StatusBarSettingHelper.statusBarLightMode(this@NotificationActivity, true)
        mAdapter = NotificationAdapter()
        mBinding.apply {
            refreshLayout?.apply {
                setEnableRefresh(true)
                setEnableLoadMore(true)
                setOnRefreshListener(this@NotificationActivity)
                setOnLoadMoreListener(this@NotificationActivity)
            }

            recyclerView.apply {
                layoutManager =
                    LinearLayoutManager(
                        this@NotificationActivity,
                        LinearLayoutManager.VERTICAL,
                        false
                    )
                adapter = mAdapter

            }
            imgClose.onClick {
                llOpenNotification.visibility = View.GONE
            }
            tvOpenNotification.onClick {
                tryJumpNotifyPage(this@NotificationActivity)
            }
        }
        mAdapter.onItemClickListener={view, position ->
            val item =mAdapter.getItem(position)
            item?.let {
                messageViewModel.updateMessage(it.id)
            }
        }


    }

    override fun initData() {
        showLoading()
        getNotificationMessage()
        messageViewModel.messageLiveData.observe(this){
            dismissLoading()
             if (mPage==1){
                 mAdapter.setData(it)
                 mBinding.refreshLayout.finishRefresh()
                 mBinding.refreshLayout.setEnableLoadMore(load(it?.size))
                 if (it.isNullOrEmpty()) {
                     mBinding.emptyView.visible()
                 } else {
                     mBinding.emptyView.gone()
                 }
             }else{
                 mBinding.refreshLayout.finishLoadMore()
                 if (!it.isNullOrEmpty()){
                     mAdapter.addAll(it)
                 }else{
                     mBinding.refreshLayout.setEnableLoadMore(false)
                 }
             }
        }
        messageViewModel.updateMessageLiveData.observe(this){
            mPage = 1
            getNotificationMessage()
            LiveDataBus.get().with(REFRESH_MESSAGE).postValue( REFRESH_MESSAGE)
        }
    }

    /**
     * 获取推荐列表数据
     */
    private fun getNotificationMessage() {
        messageViewModel.getNotificationMessage(count = mPage)

    }

    override fun onResume() {
        super.onResume()
        if (checkNotifyPermission(this@NotificationActivity)) {
            mBinding.llOpenNotification.visibility = View.GONE
        } else {
            mBinding.llOpenNotification.visibility = View.VISIBLE
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getNotificationMessage()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getNotificationMessage()
    }
}